<!--
 * @version: 
 * @Author: yu
 * @Date: 2023-07-13 15:29:44
 * @LastEditors: Andy
 * @LastEditTime: 2023-07-13 17:02:39
-->
<template>
    <el-dialog :model-value="props.isShow" :title="props.isAdd ? '添加分类' : '编辑分类'" :before-close="close">
        <el-form :model="form" :rules="rules" ref="menuRef">

            <el-form-item label="上级分类" :label-width="formLabelWidth" prop="pid">
                <el-select v-model="form.pid" placeholder="请选择上级菜单">
                    <el-option label="顶级菜单" :value="0"></el-option>

                    <el-option v-for="item in store.getters['cate/getCate']" :key="item.id" :label="item.catename"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="分类名称" :label-width="formLabelWidth" prop="catename">
                <el-input v-model="form.catename" autocomplete="off"></el-input>
            </el-form-item>

            <!-- 图片 -->
            <!-- <el-form-item label="图片" :label-width="formLabelWidth">
                <el-upload action="#" list-type="picture-card" :on-change="change" :on-preview="handlePictureCardPreview"
                    :file-list="fileList" :auto-upload="false">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item> -->

            <el-form-item v-show="form.pid !== 0" label="图片" :label-width="formLabelWidth">
                <el-upload v-model:file-list="fileList" action="#" list-type="picture-card" :on-change="change"
                    :on-preview="handlePictureCardPreview" :auto-upload="false">
                    <el-icon>
                        <Plus />
                    </el-icon>
                </el-upload>

                <el-dialog v-model="dialogVisible">
                    <img w-full :src="dialogImageUrl" alt="Preview Image" />
                </el-dialog>
            </el-form-item>




            <el-form-item label="状态" :label-width="formLabelWidth">
                <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ccc" :active-value="1"
                    :inactive-value="2">
                </el-switch>
            </el-form-item>

        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancel(menuRef)">取消</el-button>
                <el-button type="primary" @click="confirm(menuRef)">确 定</el-button>

            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps, UploadUserFile } from 'element-plus'
import { reactive, defineProps, ref, defineEmits, defineExpose } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from "element-plus";
import { cateAdd, cateInfo, cateEdit } from '@/request/api'
import { useStore } from 'vuex'
const props = defineProps({
    isShow: Boolean,
    isAdd: Boolean
})

let menuRef = ref<FormInstance>()
let typeId = 0

interface RuleForm {
    pid: string,
    catename: string,
    img: any,
    status: number
}

const rules = reactive<FormRules<RuleForm>>({
    catename: [
        { required: true, message: '请输入菜单名称', trigger: 'blur' },
        { min: 2, max: 8, message: '字符串长度是2到8', trigger: 'blur' },
    ],
    pid: [
        {
            required: true,
            message: '请输入上级菜单',
            trigger: 'change',
        },
    ],
})

const store = useStore()
store.dispatch('cate/listActions')

const emits = defineEmits(['cancel'])

let form = ref({
    pid: '',  //上级分类编号
    catename: '',  //商品分类名称
    img: '',  //图片(文件，一般用于二级分类
    status: 1, //状态        1正常2禁用
})

let dialogImageUrl = ref('')
let dialogVisible = ref(false)
let fileList: any[]


//取消
const cancel = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    emits('cancel', false)
    form.value = {
        pid: '',  //上级分类编号
        catename: '',  //商品分类名称
        img: '',  //图片(文件，一般用于二级分类
        status: 1, //状态        1正常2禁用
    }
    //清空规则验证
    formEl.resetFields()
    fileList = []
}

const close = (done: any) => {
    done;
    cancel(menuRef.value)
}

const confirm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((val) => {
        // console.log(val);
        // console.log(form.value);
        if (props.isAdd) {
            cateAdd(form.value).then((res: any) => {
                // console.log(res);
                if (res.code === 200) {
                    cancel(menuRef.value)
                    ElMessage.success(res.msg)
                    store.dispatch('cate/listActions')
                }

            })
        } else {

            cateEdit({ ...form.value, id: typeId }).then((res: any) => {
                if (res.code === 200) {
                    cancel(menuRef.value)
                    ElMessage.success(res.msg)
                    store.dispatch('cate/listActions')
                }
            })
        }


    })
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
    dialogImageUrl.value = uploadFile.url!
    dialogVisible.value = true
}
const change = (file: any) => {
    // console.log(file);
    dialogImageUrl = file.url  //取得图片的地址
    form.value.img = file.raw
}

const formLabelWidth = '120px'


//数据回显
const getDetail = (id: any) => {
    // console.log(id);
    cateInfo({ id }).then((res: any) => {
        // console.log(res);
        typeId = id
        if (res.code === 200) {
            form.value = res.list
            fileList = form.value.img ? [{ url: form.value.img }] : []
        }

    })

}

defineExpose({
    getDetail
})


</script>

<style></style>